<?php
namespace UserSystem;

use UserSystem\Driver\DB\Mysql;

/**
 * Class Log
 * 用户系统日志类
 * @author: wangxi
 */
class Logger
{
    private static $_instance;

    private function __construct()
    {

    }

    private static function getInstance()
    {
        if (!isset(self::$_instance)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * 暂时采用数据库保存日志
     * @param string $msg
     * @return bool
     */
    public static function save($msg = '', $user_id=0)
    {
//        $logger = self::getInstance();
        $dbh=Mysql::getInstance();

        $user_ip = get_user_ip();

        $user_id=intval($user_id);

        $log_stmt = $dbh->prepare("insert into `user_log`
          (`user_ip`, `user_id`, `message`, `log_time` )
          values(:user_ip, :user_id, :message, :log_time)");

        $status=$log_stmt->execute(array(
            'user_ip'  => $user_ip,
            'user_id'  => $user_id,
            'message'  => trim($msg),
            'log_time' => time()
        ));

        return $dbh->lastInsertId() > 0;
    }

}